import resources.Constants;
import registnet.dao.*;

RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/resources/registnet");


String _record = bundle.getProperty("registnet.module.logs.form.record");
String _date = bundle.getProperty("registnet.module.logs.form.date");
String _time = bundle.getProperty("registnet.module.logs.form.time");
String _logDetails = bundle.getProperty("registnet.module.logs.form.log_details");
String _check = bundle.getProperty("registnet.messages.check");
String recordValue;
Object log;
List logs;
List fields;

private class LogRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		for(int i=0; i<data.length; i++){
			new Label(data[i]).setParent(row);	
		}
	}
}

void retrieveLogs(){
	try{
		String con = constraint.getSelectedItem().getLabel();
		recordValue = record.getSelectedItem().getLabel();
		
		if(recordValue.equals("College")){
			fields = new College().getFields();
			log = new CollegeLogPeer();
		}
		else if(recordValue.equals("Unit")){
			fields = new Unit().getFields();
			log = new UnitLogPeer();
		}
		else if(recordValue.equals("Course")){
			fields = new Course().getFields();
			log = new CourseLogPeer();
		}
		else if(recordValue.equals("Degree Program")){
			fields = new DegreeCourse().getFields();
			log = new DegreeCourseLogPeer();
		}
		else if(recordValue.equals("Curriculum")){
			fields = new Curriculum().getFields();
			log = new CurriculumLogPeer();
		}
		else if(recordValue.equals("Student")){
			fields = new Student().getFields();
			log = new StudentLogPeer();
		}
		else if(recordValue.equals("Grade")){
			fields = new Grade().getFields();
			log = new GradeLogPeer();
		}
		
		if(con.equals("Day")){
			logs = getLogsByDay();
		}
		else if(con.equals("Time")){
			logs = getLogsByTime();
		}
		
		if(logs.size()!=0){
			int fc = 0; 
			for(int i=0; i<logs.size(); i++){
				if(logs.get(i).getAction()==4)
					fc++;
			}
			String[][] listModel = new String[logs.size()-fc][5];
			int i=0;
			for(j = 0; j < logs.size(); j++){
				Object obj = logs.get(j);
				int action = obj.getAction();
				if(action!=4){
					listModel[i][0] = obj.getMonth()+"-"+obj.getDay()+"-"+obj.getYear();
					listModel[i][1] = obj.getHour()+":"+obj.getMinute()+":"+obj.getSecond();
					HashMap actionMap;
					actionMap = Constants.MAP_LOG_ACTION_VALUE;
					String[] attrib = obj.getAttributes();
					String msg;
					if(action==1){
						msg = actionMap.get(action) + " new " + recordValue.toUpperCase() +" " +attrib[0]+" "+attrib[1];
						if(log instanceof GradeLogPeer){
							msg = msg + " Grade=\"" + obj.getGrade()+"\"";
						}
					}
					else if(action==2){
						String[] newVal = logs.get(j-1).getAttributes();
						String changes = getChanges(attrib, newVal);
						msg = actionMap.get(action)+ " " + recordValue.toUpperCase() + " " +obj.getIdentifiers()+ " "+changes;
					}
					else if(action==3){
						msg = actionMap.get(action)+ " " + recordValue.toUpperCase() + " " +attrib[0]+" "+attrib[1];
					}
					listModel[i][2] = msg;
					listModel[i][3] = obj.getUser();
					listModel[i][4] = obj.getLogUnitCode();
					i++;
				}
				
			}
			ListModel strset = new SimpleListModel(listModel);
			contents_win.getFellow("logs_g").setModel(strset);
			contents_win.getFellow("logs_g").setRowRenderer(new LogRenderer());
			showRollbackPrompt();
			
		}
		else{
			contents_win.setVisible(false);
			prompt.setVisible(false);
			alert("Nothing to rollback.");
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	
}

List getLogsByDay(){
	
	List logs;
	Integer year;
	Integer month;
	Integer days;
	Date d = day.getValue();
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	
	if(d!=null){
		String[] date = sdf.format(d).split("-");
		year = Integer.parseInt(date[0]);
		month = Integer.parseInt(date[1]);
		days = Integer.parseInt(date[2]);
		
		logs = log.searchLogsBeforeDate(year,month,days);
	}
	else
		alert(_check);
		
	return logs;
}


List getLogsByTime(){
	List logs;
	Integer year;
	Integer month;
	Integer day1;
	Date d = day.getValue();
	Integer t = time.getValue();
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	if(d!=null){
		String[] date = sdf.format(d).split("-");
		year = Integer.parseInt(date[0]);
		month = Integer.parseInt(date[1]);
		day1 = Integer.parseInt(date[2]);
		
		if(t!=null && t<24){
			logs = log.searchLogsBeforeDateAndTime(year, month, day1, t);
		}
		else
			alert(_check);
	}
	else
			alert(_check);
	return logs;
}


doSearch(String cons){
	record.setSelectedIndex(0);
	record.setVisible(true);
	recordV.setVisible(true);
	go.setVisible(true);
	
	if(cons.equals("Day")){
		day.setVisible(true);
		time.setVisible(false);
		dayV.setVisible(true);
		timeV.setVisible(false);
	}
	else if(cons.equals("Time")){
		
		day.setVisible(true);
		time.setVisible(true);
		
		dayV.setVisible(true);
		timeV.setVisible(true);
	}
}

showRollbackPrompt(){
	contents_win.setVisible(true);
	prompt.setVisible(true);
}

proceedRollback(){
	Vector logIds = null;
	logIds = log.rollback(logs);
	contents_win.setVisible(false);
	prompt.setVisible(false);
	if(logIds==null){
		alert("Rollback successful");
		SystemLogPeer.createNewSystemLog("Rollback was done on "+recordValue,"registnet","Administrator");
	}
	else{
		log.cancelRollback(logIds);
		alert("Rollback failed");
	}	
}

cancelRollback(){
	contents_win.setVisible(false);
	prompt.setVisible(false);
}

String getChanges(String[] oldVal, String[] newVal){
	String changes="";
	for(int i=0; i<oldVal.length; i++){
		if(fields.get(i).equals("Photo")){
			//changes=changes+"Photo TO new photo ";
		}
		else if(fields.get(i).equals("Signature")){
			//changes=changes+"Signature TO new signature ";
		}
		else if(oldVal[i]!=null && newVal[i]!=null){
			if(oldVal[i].compareTo(newVal[i])!=0){
				changes = changes+fields.get(i)+"=\""+oldVal[i]+"\" TO "+"\""+newVal[i]+"\" ";
			}
		}
		else if((oldVal[i]==null&&newVal[i]!=null) || (oldVal[i]!=null&&newVal[i]==null))
			changes = changes+fields.get(i)+"=\""+oldVal[i]+"\" TO "+"\""+newVal[i]+"\" ";
	}
	return changes;
}